package genetic.core;

import info.monitorenter.gui.chart.IAxis.AxisTitle;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.ZoomableChart;
import info.monitorenter.gui.chart.rangepolicies.RangePolicyFixedViewport;
import info.monitorenter.gui.chart.traces.Trace2DLtd;
import info.monitorenter.util.Range;

import java.awt.Color;
import java.util.Observable;
import java.util.Observer;

/**
 * Evolution progress monitoring tool.
 * 
 * @author vasek
 *
 */
public class BreedMonitor extends ZoomableChart implements Observer {

	/**
	 * Serial version UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor.
	 * 
	 * @param maxGenerations how many generations to show on x-axis.
	 */
	public BreedMonitor(int maxGenerations) {
		ITrace2D trace = new Trace2DLtd(maxGenerations);
		trace.setColor(Color.RED);
	    setUseAntialiasing(true);
		addTrace(trace);
		getAxisX().setAxisTitle(new AxisTitle("generation"));
		getAxisY().setAxisTitle(new AxisTitle("fitness"));
        getAxisX().setRangePolicy(new RangePolicyFixedViewport(new Range(0, maxGenerations)));
	}

	@Override
	public void update(Observable algorithm, Object obj) {
		final PopulationChangedEvent event = (PopulationChangedEvent) obj;
        final Trace2DLtd trace = (Trace2DLtd) getTraces().first();
        trace.addPoint(event.getGeneration(), event.getFitness());
	}
}
